﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace chromemeter
{
    public partial class FormSetting : Form
    {
        public FormSetting()
        {
            InitializeComponent();
        }

        #region 变量声明

        private double voltLimit;
        private double currMax;
        private double currMin;
        private double currSpace;
        private int vTimeSpace;
        private int vCount;

        private double currLimit;
        private double voltMax;
        private double voltMin;
        private double voltSpace;
        private int iTimeSpace;
        private int iCount;
        /*private bool isL6 = false;
        private bool isE1 = false;
        private bool isJIV = false;
        private bool isUI= false;*/


        public double VoltLimit
        {
            get { return voltLimit; }
            set { voltLimit = value; }
        }
        public double CurrSpace
        {
            get { return currSpace; }
            set { currSpace = value; }
        }
        public double CurrMax
        {
            get { return currMax; }
            set { currMax = value; }
        }
        public double CurrMin
        {
            get { return currMin; }
            set { currMin = value; }
        }

        public int VTimeSpace
        {
            get { return vTimeSpace; }
            set { vTimeSpace = value; }
        }

        public int VCount
        {
            get { return vCount; }
            set { vCount = value; }
        }

        public double CurrLimit
        {
            get { return currLimit; }
            set { currLimit = value; }
        }
        public double VoltSpace
        {
            get { return voltSpace; }
            set { voltSpace = value; }
        }
        public double VoltMax
        {
            get { return voltMax; }
            set { voltMax = value; }
        }
        public double VoltMin
        {
            get { return voltMin; }
            set { voltMin = value; }
        }

        public int ITimeSpace
        {
            get { return iTimeSpace; }
            set { iTimeSpace = value; }
        }
        public int ICount
        {
            get { return iCount; }
            set { iCount = value; }
        }
        /* public bool IsL6
         {
             get { return isL6; }
             set { isL6 = value; }
         }

         public bool IsE1
         {
             get { return isE1; }
             set { isE1 = value; }
         }
         */
        #endregion

        #region 功能函数

        private static void txtDataVerifyWithOnePoint(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount == 1)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        #endregion

        #region 窗体事件
        private void Formlsetting_Load(object sender, EventArgs e)
        {
            txtVoltLimit.Text = voltLimit.ToString();
            txtCurrMax.Text = currMax.ToString();
            txtCurrMin.Text = currMin.ToString();
            txtCurrSpace.Text = currSpace.ToString();
            txtItimespace.Text = iTimeSpace.ToString();
            txtIcount.Text = iCount.ToString();

            txtCurrLimit.Text = currLimit.ToString();
            txtVoltMax.Text = voltMax.ToString();
            txtVoltMin.Text = voltMin.ToString();
            txtVoltSpace.Text = voltSpace.ToString();
            txtVtimespace.Text = vTimeSpace.ToString();
            txtVcount.Text = vCount.ToString();

            /*if (isL6 || isE1)
            {
                lblCurrSet.Text = "电    流(A)";
                lblCurrMin.Visible = false;
                lblCurrSpace.Visible = false;
                txtCurrMin.Visible = false;
                txtCurrSpace.Visible = false;
            }*/
        }
        private void Formlsetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            voltLimit = double.Parse(txtVoltLimit.Text);
            currMax = double.Parse(txtCurrMax.Text);
            currMin = double.Parse(txtCurrMin.Text);
            currSpace = double.Parse(txtCurrSpace.Text);
            iTimeSpace = int.Parse(txtItimespace.Text);
            iCount = int.Parse(txtIcount.Text);

            currLimit = double.Parse(txtCurrLimit.Text);
            voltMax = double.Parse(txtVoltMax.Text);
            voltMin = double.Parse(txtVoltMin.Text);
            voltSpace = double.Parse(txtVoltSpace.Text);
            vTimeSpace = int.Parse(txtVtimespace.Text);
            vCount = int.Parse(txtVcount.Text);
        }

        #endregion

        #region 按钮事件
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
        #endregion

        #region 其它事件

        private void txtVoltLimit_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVoltLimit);
        }

        private void txtCurrMax_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtCurrMax);
        }

        private void txtCurrMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtCurrMin);
        }

        private void txtCurrSpace_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtCurrSpace);
        }



        private void txtItimespace_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtItimespace);
        }

        private void txtIcount_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtIcount);
        }

        private void txtCurrLimit_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtCurrLimit);
        }

        private void txtVoltMax_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVoltMax);
        }

        private void txtVoltMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVoltMin);
        }

        private void txtVoltSpace_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVoltSpace);
        }



        private void txtVtimespace_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVtimespace);
        }

        private void txtVcount_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtDataVerifyWithOnePoint(e, txtVcount);
        }

        #endregion
    }
}
